package org.medicalinstrument.service;

import net.sf.json.JSONArray;
import org.medicalinstrument.bean.PurchaseOrderDetail;
import org.medicalinstrument.bean.PurchaseOrderMaster;
import org.medicalinstrument.dao.PurchaseOrderDao;
import org.medicalinstrument.database.DataSourceUtils;
import org.medicalinstrument.impl.PurchaseOrderDaoImpl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class PurchaseOrderService {
    private String returnString;
    PurchaseOrderDao pod = new PurchaseOrderDaoImpl();
    List purchaseorderlist = null;
    int modifyResult=0;

/**
 * @desc ： 得到所有订单信息
 * @author : llh
 * @param :
 * @return :
**/
    public String getAllOrder() throws Exception{
        purchaseorderlist =  pod.getAllPurchaseOrderList();
        if(purchaseorderlist.size() <= 0){
            returnString = "nopurchaseorder";
        }else{
            returnString = JSONArray.fromObject(purchaseorderlist).toString();
        }
        return returnString;
    }
    /**
     * @desc ： 条件查询订单
     * @author : llh
     * @param :
     * @param
     * @return :
    **/

    public String optionSelect(String accountNo, String timein, String timeout) throws SQLException {
        purchaseorderlist =  pod.optionSelect(accountNo,timein,timeout);
        System.out.println(purchaseorderlist);
        if(purchaseorderlist.size() <= 0){
            returnString = "nopurchaseorder";
        }else{
            returnString = JSONArray.fromObject(purchaseorderlist).toString();
        }
        System.out.println(returnString);
        return returnString;
    }

    public String modifyOrder(String custPhone,String getOrConsigee, String state, String consigeeAddr,String orderId,String accountNo,String productQR) throws SQLException {
        modifyResult = pod.modifyOrder(custPhone,getOrConsigee, state, consigeeAddr,orderId,accountNo,productQR);
        if (modifyResult <= 0) {
            returnString = "nopurchaseorder";
        } else {
            returnString = "success";
        }
        return returnString;
    }

    public String delete(String orderId, String accountNo) throws SQLException {
        modifyResult = pod.delete(orderId,accountNo);
        if (modifyResult <= 0) {
            returnString = "nopurchaseorder";
        } else {
            returnString = "success";
        }
        return returnString;
    }

    /**
     *@Author cirly
     *@Description work after buy
     *@Date 13:10 2019/3/24
     *@Modify by
     *@Version 1.0
     */
    public String buyShoppingCart(PurchaseOrderMaster pm, List<PurchaseOrderDetail> detailList) throws SQLException {
        //PurchaseOrderDao
        Connection conn = null;
        try {
            //获得连接
            conn = DataSourceUtils.getConnection();
            //设置事务不自动提交
            conn.setAutoCommit(false);
            DataSourceUtils.startTransaction();
            if(pod.PurchaseMaster_add(conn,pm)){
                for(PurchaseOrderDetail orderDetail : detailList){
                    pod.PurchasrDetail_add(conn,orderDetail);
                    //修改订单状态
                    //orderDetail.setOrderState("1");
                    //detailDao.updateOrderState(conn,orderDetail);
                    pod.updateShoppingCart(orderDetail);
                    //修改商品状态
                   // proDao.updateState(conn,1,orderDetail.getProductQR());
                }
                returnString = "insert_success";
                DataSourceUtils.commitAndRelease();
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnString = "insert_fail";
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        return returnString;
       // return sd.buy(masterList,detailList);
    }


    public String geCustomerOrder(String accountNo)throws SQLException{
        List list = pod.getOrderByCustomer(accountNo);
        System.out.println(list.size());
        for(int i = 0 ; i < list.size() ; i ++){
            System.out.println(list.get(i));
        }
        if(list.size()>0){
            return JSONArray.fromObject(list).toString();
        }else{
            return "purchase_null";
        }
    }
    /**
     * @desc ： 订单详细信息
     * @author : llh
     * @param :
     * @param orderId
     * @return :
     **/
    public String getOrderDetail(String orderId,String accountNo) throws SQLException{
        purchaseorderlist =  pod.getOrderDetail(orderId,accountNo);
        if(purchaseorderlist.size() <= 0){
            returnString = "nopurchaseorder";
        }else{
            returnString = JSONArray.fromObject(purchaseorderlist).toString();
        }
        return returnString;
    }

    public String optionsSelect(String name, String accountNo, String timein, String timeout) throws SQLException{
        purchaseorderlist =  pod.optionsSelect(name,accountNo,timein,timeout);
        if(purchaseorderlist.size() <= 0){
            returnString = "nopurchaseorder";
        }else{
            returnString = JSONArray.fromObject(purchaseorderlist).toString();
        }
        return returnString;
    }
}
